package com.sfx.shortLink.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sfx.shortLink.dao.entity.LinkLocaleStatsDO;
import com.sfx.shortLink.dto.LinkLocaleStatsDTO;
import com.sfx.shortLink.dto.req.ShortLinkGroupStatsReqDTO;
import com.sfx.shortLink.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ILinkLocaleStatsMapper extends BaseMapper<LinkLocaleStatsDO> {


    // unique (full_short_url, gid, date, adcode, province)
    @Insert("INSERT INTO t_link_locale_stats(full_short_url, gid, date, cnt, province, city, adcode, country, create_time, update_time, del_flag)" +
            "VALUES( #{linkLocaleStats.fullShortUrl}, #{linkLocaleStats.gid}, #{linkLocaleStats.date}, #{linkLocaleStats.cnt},#{linkLocaleStats.province}," +
            "#{linkLocaleStats.city}, #{linkLocaleStats.adcode}, #{linkLocaleStats.country}, NOW(), NOW(), 0) ON DUPLICATE KEY UPDATE " +
            "cnt = cnt + #{linkLocaleStats.cnt};")
    void LocaleStats(@Param("linkLocaleStats") LinkLocaleStatsDO linkLocaleStats);


    /**
     *
     */
    @Select("SELECT " +
            "province, " +
            "city, " +
            "count(province) as cnt " +
            "FROM t_link_locale_stats " +
            "where full_short_url = #{param.fullShortUrl} and gid = #{param.gid} and date between #{param.startDate} and #{param.endDate} " +
            "group by full_short_url, gid, province")
    List<LinkLocaleStatsDTO> listLocaleCNByShortLink(@Param("param") ShortLinkStatsReqDTO param);//statsByShortLinkGroup

    /**
     *
     */
    @Select("SELECT " +
            "province, " +
            "city, " +
            "count(province) as cnt " +
            "FROM t_link_locale_stats " +
            "where gid = #{param.gid} and date between #{param.startDate} and #{param.endDate} " +
            "group by gid, province")
    List<LinkLocaleStatsDTO> listLocaleCNByShortLinkGroup(@Param("param") ShortLinkGroupStatsReqDTO param);

}
